package com.example.criminalintent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;

import com.example.criminalintent.fragments.CrimeFragment;
import com.example.criminalintent.model.Crime;
import com.example.criminalintent.model.CrimeLab;

import java.util.List;
import java.util.UUID;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class CrimePagerActivity extends AppCompatActivity {
    private static String EXTRA_CRIME_ID="uuid";
    private ViewPager mViewPager;
    private List<Crime> mCrimes;

    private Button mFirstButton;
    private Button mLastButton;
    private int position;

    public static Intent newIntent(Context packageContext, UUID crimeId){
        Intent intent = new Intent(packageContext,CrimePagerActivity.class);
        intent.putExtra(EXTRA_CRIME_ID,crimeId);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crime_pager);
        mViewPager = findViewById(R.id.activity_crime_pager_view_pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        mCrimes = CrimeLab.getsCrimeLab(this).getCrimes();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                Crime crime = mCrimes.get(position);
                return CrimeFragment.newInstance(crime.getmId());
            }

            @Override
            public int getCount() {
                return mCrimes.size();
            }
        });
        UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
         int position_fromActivity = getIntent().getIntExtra("position",-1);
        if(position_fromActivity == -1){
            position = mCrimes.size()-1;
            mViewPager.setCurrentItem(mCrimes.size()-1);
        }else {
            mViewPager.setCurrentItem(position_fromActivity);
        }

        mFirstButton = findViewById(R.id.first_button);
        mLastButton = findViewById(R.id.last_button);
        forButton();
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                CrimePagerActivity.this.position = position;
                forButton();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mFirstButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(0);
            }
        });

        mLastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(mViewPager.getAdapter().getCount()-1);
            }
        });
    }


    private void forButton(){
        mFirstButton.setVisibility(View.VISIBLE);
        mLastButton.setVisibility(View.VISIBLE);
        if(position == 0){
            mFirstButton.setVisibility(View.GONE);
        }else if(position == mViewPager.getAdapter().getCount()-1){
            mFirstButton.setVisibility(View.VISIBLE);
            mLastButton.setVisibility(View.GONE);
        }else {
            mFirstButton.setVisibility(View.VISIBLE);
            mLastButton.setVisibility(View.VISIBLE);
        }

    }

    public int getPosition(){
        return position;
    }


}
